<?php
declare (strict_types = 1);

namespace app\index\controller;

use app\index\BaseController;
use app\index\model\ChinaCity;
use think\facade\Db;
use think\facade\Request;
use think\facade\Session;
use think\facade\View;

class Address extends BaseController
{
    //收货地址
    public function address()
    {
        $list = \app\index\model\Address::addressList();
        $count = count($list);
        View::assign('count',$count);
        View::assign('list',$list);
        return View::fetch();
    }
    //删除地址
    public function addressDel()
    {
        $post = Request::post();
        $res = \app\index\model\Address::destroy($post['id']);
        return $res;
    }
    //默认地址
    public function ajaxisDefault()
    {
        $post = Request::post();
        // 启动事务
        Db::startTrans();
        try{
            $res = \app\index\model\Address::where('id','<>',$post['id'])->where('uid','=',Session::get('user'))->update(['is_default'=>0]);
            $res = \app\index\model\Address::where('id','=',$post['id'])->where('uid','=',Session::get('user'))->update(['is_default'=>1]);
            // 提交事务
            Db::commit();
        }catch (\Exception $e){
            $res = false;
            // 回滚事务
            Db::rollback();
        }
        return $res;
    }

    //收货地址添加
    public function addressAdd()
    {
        $sheng = ChinaCity::where('tid','=',0)->select();
        View::assign('sheng',$sheng);
        if (Request::isPost()){
            $post = Request::post();
            $get = Request::get();
            if (isset($get) && !empty($get)){
                $return = $get['return'];
            }
            $isDefault = \app\index\model\Address::where('uid','=',Session::get('user'))->select()->toArray();
            if (isset($isDefault) && !empty($isDefault)){
                $data = [
                    'name'=>$post['name'],
                    'tel'=>$post['tel'],
                    'sheng'=>$post['sheng'],
                    'city'=>$post['city'],
                    'quyu'=>$post['quyu'],
                    'address'=>$post['address'],
                    'code'=>0,
                    'uid'=>Session::get('user'),
                    'is_default'=>0,
                ];
            }else{
                $data = [
                    'name'=>$post['name'],
                    'tel'=>$post['tel'],
                    'sheng'=>$post['sheng'],
                    'city'=>$post['city'],
                    'quyu'=>$post['quyu'],
                    'address'=>$post['address'],
                    'code'=>0,
                    'uid'=>Session::get('user'),
                    'is_default'=>1,
                ];
            }
            list($res,$msg) = \app\index\model\Address::addAddressInfo($data);

            if ($res){
                $this->success('添加成功',url('Address/address'));
            }else{
                $this->error($msg);
            }
        }
        return View::fetch();
    }
    //市
    public function addressSheng()
    {
        $post = Request::post();
        if (!empty($post['sheng']) && isset($post['sheng'])){
            $city = ChinaCity::where('tid','=',$post['sheng'])->select();
        }
        return json($city);
    }
    //区
    public function addressCity()
    {
        $post = Request::post();
        if (!empty($post['city']) && isset($post['city'])){
            $quyu = ChinaCity::where('tid','=',$post['city'])->select();
        }
        return json($quyu);
    }
    //收货地址修改
    public function addressEdit()
    {
        if (Request::isPost()){
            $post = Request::post();
            $edit = \app\index\model\Address::editAddressInfo($post);
            if ($edit){
                $this->success('修改成功',url('Address/address'));
            }else{
                $this->error('修改失败');
            }
        }else{
            $get = Request::get();
            $find = \app\index\model\Address::alias('a')
                ->field('a.*,cc.name as cname,ccc.name as qname')
                ->where('a.id','=',$get['id'])
                ->join('china_city cc','cc.id=a.city')
                ->join('china_city ccc','ccc.id=a.quyu')->find()->toArray();
            View::assign($find);
            $sheng1 = ChinaCity::where('tid','=',0)->select();
            View::assign('sheng1',$sheng1);
        }
        return View::fetch();
    }


}